<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta name="format-detection" content="telephone=no" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-touch-fullscreen" content="yes">
    <meta content="telephone=no,email=no" name="format-detection" />
    <meta name="flexible" content="initial-dpr=2,maximum-dpr=3" />
    <!--去缓存-->
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
    <META HTTP-EQUIV="Expires" CONTENT="0"><!--end-->
    <title>羊仓健康生活家</title>
    <link rel="stylesheet" href="../css/gwcfs.css" />
    <link rel="stylesheet" href="../css/style.css" />
    <script type="text/javascript" src="../js/flexible.js" ></script>
    <script type="text/javascript" src="../js/jquery.min.js" ></script>
    <script type="text/javascript" src="../js/basePathUtil.js" ></script>
    <!--货币格式化-->
    <script type="text/javascript" src="../js/jquery-formatcurrency/jquery.formatCurrency.js" ></script>
    <!--弹出-->
    <script type="text/javascript" src="../js/layer-mobile/layer.js" ></script>
    <!--微信支付-->
    <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

    <script type="text/javascript">
        var basePath=basePath();
        var access_token = GetUrl("access_token");
        //声明支付类型全局变量
        var payTypeName = 'wechat';
        $(function(){
            $("#wechat").find("img").show();
            //判断是否支持微信支付,不支持就隐藏掉
            if (typeof WeixinJSBridge == "undefined"){
//                $("#wechat").hide();
//                $("#wechat").find("img").hide();
//                $("#alipay").find("img").show();
//                payTypeName="alipay";
            }
            //设置默认支付状态的样式
            $(".weizhilist").click(function(){
                $(".zfjpg > img").hide();
                $(this).find('img').show();
                payTypeName = $(this).attr('id');
            });
        });
    </script>
</head>
<body>
    <div class="warp">
        <div class="top">
            <p>请选择支付方式</p>
        </div>
        <ul class="weizhi">
            <li class="weizhilist" id="wechat">
                <div class="leftzf">
                    <div class="zfradiu"></div>
                    <div class="zfjpg">
                        <img src="../img/good.png">
                    </div>
                </div>
                <div class="listzhi">
                    <p>微信</p>
                </div>
            </li>
            <li class="weizhilist" id="alipay">
                <div class="leftzf">
                    <div class="zfradiu"></div>
                    <div class="zfjpg">
                        <img src="../img/good.png">
                    </div>
                </div>
                <div class="listzhi">
                    <p>支付宝</p>
                </div>
            </li>
        </ul>
        <!--确定-->
        <div class="btn"></div>
    </div>
<script>
    function onBridgeReady(appId,timeStamp,nonceStr,package,signType,paySign){
        WeixinJSBridge.invoke(
                'getBrandWCPayRequest', {
                    "appId":appId,     //公众号名称，由商户传入
                    "timeStamp":timeStamp,         //时间戳，自1970年以来的秒数
                    "nonceStr":nonceStr, //随机串
                    "package":package,
                    "signType":signType,         //微信签名方式：
                    "paySign":paySign //微信签名
                },
                function(res){
                    if(res.err_msg == "get_brand_wcpay_request:ok" ) {
//                        window.location.href=basePath+"res/html/zfsuccess.html";
                        window.location.href=basePath+"res/orderDetail.html?access_token="+access_token+'&tradeId='+GetUrl("tradeId")+"&t="+new Date().getTime();
                    }     // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
                }
        );
    }
    function payment(id) {
        if(id=="" || id=="null" || id==null || id<1){
            layer.open({content: '订单不存在',skin: 'msg',time: 2});return;
        }
        if(payTypeName != "alipay" && payTypeName != "wechat"){
            layer.open({content: '请选择支付方式',skin: 'msg',time: 2});return;
        }
        $.ajax({
            type:"POST",
            url:basePath+"/v1/trade/api/continuePay?access_token="+access_token,
            dataType : 'json',
            data:{
                tradeId:id,
                payType:payTypeName=="alipay"?3:1
            },
            success:function(data){
                if(data.code==1000) {
                    if(payTypeName=="alipay"){
                       var tradePayParam = data.data.tradePayParam;
                        window.location.href="https://openapi.alipay.com/gateway.do?"+tradePayParam;
                    }
                    if(payTypeName=="wechat"){
                        var tradePayParam = data.data.tradePayParam;
                        onBridgeReady(tradePayParam.appId,tradePayParam.timeStamp,tradePayParam.nonceStr,tradePayParam.package,tradePayParam.signType,tradePayParam.paySign);
                    }
                }else{
                    layer.open({content: data.msg,skin: 'msg',time: 2});
                }
            },
            error:function(data) {
            }
        });
    }
    /**
     * 显示订单详细
     * @param id
     */
    function tradeDetailShow(id){
        if(id=="" || id=="null" || id==null || id<1 || id==undefined || id=='undefined'){
            layer.open({content: '订单不存在',skin: 'msg',time: 2});return;
        }
        $.ajax({
            type:"GET",
            url:basePath+"/v1/trade/api/showTrade?access_token="+access_token,
            dataType : 'json',
            data:{
                tradeId:id
            },
            success:function(data){
                if(data.code==1000) {
                    localStorage.clear();
                    localStorage.setItem("tradeId", id);
                    localStorage.setItem("access_token", access_token);

                    if(data.data.tradeSummary.status == "S"){
                        if(data.data.tradeSummary.payStatus == "U"){
                            $(".btn").html('<button onclick="payment('+data.data.tradeSummary.id+')">确定</button>');
                        }else{
                            layer.open({content: "订单已支付，无需重复支付",skin: 'msg',time: 2});
                        }
                    }else{
                        layer.open({content: "该订单状态无法发起付款",skin: 'msg',time: 2});
                    }
                }else{
                    layer.open({content: data.msg,skin: 'msg',time: 2});
                }
            },
            error:function(data) {
            }
        });
    }
    tradeDetailShow(GetUrl("tradeId"));
</script>
</body>
</html>
